#!/usr/bin/env python
# -*- coding: utf-8 -*-
# wwq @ 2013-11-05 11:35:35

import os
import sys
import os.path
import time
from datetime import datetime

ROOT = os.path.dirname(os.path.abspath(__file__))
OUTPUT_DIR = os.path.join(ROOT, '../output')
SRC_DIR = os.path.join(ROOT, '../_src')

def md_title(title, filename):
    now = datetime.now()
    this_time = now.strftime('%Y-%m-%d')
    if not os.path.isdir(OUTPUT_DIR):
        os.mkdir(SRC_DIR)

    doc = '''Title: %s\nDateTime: %s\n\n''' % (title, this_time)
    
    f = open(os.path.join(SRC_DIR, filename), 'w')
    f.write(doc)
    f.close()

def create_md_file(title):
    '''创建一个以后标题为名称的.md文件
    @title: 以'filename'的形式创建的filenane.md文件
    '''
    t = title.split(' ')
    base_name = '-'.join(t)
    filename = base_name + '.md'
    if os.path.exists(filename):
        pass ## TODO 文件已存在时，需另外创建一个文件
    
    md_title(title, filename)
    print '%s 文件已创建，你可以打开文件进行编辑' % os.path.dirname(filename)

    return filename

def test():
    title = 'hello world'
    filename = create_md_file(title)
    print filename

if __name__ == '__main__':
    test()


